/**
 * 施肥
 */
class ManurePanel extends PanelBase {

	public constructor() {
		super();
		this.skinName = ManureSkin;
	}


	// 拥有化肥数量
	private labHas: eui.Label;
	// 剩余时间
	private labTime: eui.Label;
	// 使用次数
	private labNum: eui.Label;
	// 分享按钮
	private btnShare: eui.Button;
	// 施肥按钮
	private btnManure: eui.Button;

	private interval_id;



	protected createChildren() {
		super.createChildren();

		// 设置标题
		this.commonPanel.hideIcon();
		this.commonPanel.setTitle("title_manure_png");

		// 初始化显示
		this.initShowInfo();

		// 获取化肥信息
		Net.ins.send(['fertilizer'], {}, (e) => {
			PlayerSelfData.manure['expire'] = e['expire'];
			PlayerSelfData.manure['number'] = e['number'];
			PlayerSelfData.manure['today_use'] = e['today_use'];
			this.initShowInfo();
			this.interval_id = egret.setInterval(this.showTime, this, 1000);
		});

		// 监听点击
		this.btnShare.addEventListener(egret.TouchEvent.TOUCH_TAP, this.shareTap, this);
		this.btnManure.addEventListener(egret.TouchEvent.TOUCH_TAP, this.manureTap, this);
	}


	/**
	 * 点击分享
	 */
	private shareTap() {
		SceneManager.ins.popScene();
		MainMenu.ins.shareRepeat(3, s => {
			if (s) {
				SceneManager.ins.alert("分享成功");
				Net.ins.send(['fertilizerAdd'], {}, (e) => {
					PlayerSelfData.manure['number']++;
					this.initShowInfo();
				});
			}
		})
	}



	/**
	 * 点击施肥
	 */
	private manureTap() {
		if (PlayerSelfData.manure['today_use'] < 4) {
			Net.ins.send(['fertilizerUse'], {}, (e) => {
				PlayerSelfData.manure['expire'] = Utils.time() + 10800;
				PlayerSelfData.manure['number']--;
				PlayerSelfData.manure['today_use']++;
				this.initShowInfo();
				egret.clearInterval(this.interval_id);
				this.interval_id = egret.setInterval(this.showTime, this, 1000);
				EventManager.instance.dispatch(EventName.MANURE_STATUS_CHANGE, PlayerSelfData.manure['expire']);
			});
		}
	}


	/**
	 * 初始化显示
	 */
	private initShowInfo() {
		this.labHas.text = PlayerSelfData.manure["number"];
		this.labNum.text = PlayerSelfData.manure["today_use"] + "/4次";
	}


	/**
	 * 剩余时间处理
	 */
	private showTime() {
		if (PlayerSelfData.manure['expire'] > Utils.time()) {
			var txt = "";
			var time = new Date((PlayerSelfData.manure['expire'] - Utils.time()) * 1000);

			if (PlayerSelfData.manure['expire'] >= 3600) {
				txt += (time.getHours() - 8) + "时"
			}
			if (PlayerSelfData.manure['expire'] >= 60) {
				txt += time.getMinutes() + "时"
			}
			txt += time.getSeconds() + "秒"
			this.labTime.text = txt;
		} else {
			this.labTime.text = "0";
			egret.clearInterval(this.interval_id);
		}
	}

}